// [[ OnflowGenerator File UUID: 6e39e524f2c84852b295230cce144c14  ]]
package com.itdct.onflow.system.role.service

import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.KeyLabelValueVo
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.role.entity.SysRoleMenuEntity
import com.itdct.onflow.system.role.mapper.SysRoleMenuMapper
import com.itdct.onflow.system.role.qo.SysRoleMenuEditQo
import com.itdct.onflow.system.role.qo.SysRoleMenuSearchQo
import com.itdct.onflow.system.role.vo.SysRoleMenuDetailVo
import com.itdct.onflow.system.role.vo.SysRoleMenuListVo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-09-01 14:06:58
 * @version 1.0
 * @description 角色菜单关联服务
 */
@Service
class SysRoleMenuService(
    val sysRoleMenuMapper: SysRoleMenuMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysRoleMenuEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysRoleMenuEntity::class.java)
        val id = sysRoleMenuMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysRoleMenuEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysRoleMenuMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysRoleMenuMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysRoleMenuEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysRoleMenuMapper.selectByIdsEx(ids)
        sysRoleMenuMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysRoleMenuSearchQo, context: Context): RespVo<SysRoleMenuDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysRoleMenuMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("角色菜单关联", id))
        return success(vo.dict())
    }

    fun page(qo: SysRoleMenuSearchQo, context: Context): RespPageVo<SysRoleMenuListVo> {
        startPage(context)
        val pageData = sysRoleMenuMapper.findByList(qo)
        return successPage(pageData.dict())
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

    fun selectRoleIdList(qo: SysRoleMenuSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleMenuMapper.selectRoleIdList(qo)
        return success(vos)
    }

    fun selectMenuIdList(qo: SysRoleMenuSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleMenuMapper.selectMenuIdList(qo)
        return success(vos)
    }
// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}